package com.platform.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.constants.SmsPlatformConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ly
 * @version 1.0
 * @ClassName: SmsUtil
 * @Description: 短信工具
 * @date 2018年12月25日 下午6:24:50
 */
public class DlUtil {

    private static final Logger logger = LoggerFactory.getLogger(DlUtil.class);


    /**
     * @Title: sendMessage
     * @Description: DL发送短信
     * @return int 1成功 -1失败
     * @date 2019年1月9日 下午5:14:54
     * @param phone
     * @param content
     * @param userName
     * @param password
     * @return
     */
/*	@SuppressWarnings("unchecked")
	public static int sendMessage(String phone, String content, String userName, String password) {
		HxHttpConnManager httpConnectionManager = new HxHttpConnManager();
		CloseableHttpClient httpClient = httpConnectionManager.getHttpClient();
		HttpPost httppost = new HttpPost(SmsPlatformConstants.DL_SMS_URL);
		int sta = -1;
		try {
			// 生成发送短信的实体
			List<NameValuePair> nameValuePairs = getSendParams(phone, content, userName, password);
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			CloseableHttpResponse response = null;
			response = httpClient.execute(httppost);
			int status = response.getStatusLine().getStatusCode();
			if (status >= 200 && status < 300) {
				HttpEntity entity = response.getEntity();
				String msg = EntityUtils.toString(entity, "UTF-8");
				String json = msg.replace("[", "").replace("]", "");
				Map<String, Object> obj = JSON.parseObject(json, Map.class);
				String stsss = (String) obj.get("code");
				if (stsss != null && stsss.equals("SUCCESS"))
					sta = 1;
			} else {
				httppost.abort();
			}
		} catch (Exception e) {
			httppost.abort();
			e.printStackTrace();
		}
		return sta;
	}*/

	/*@SuppressWarnings("unused")
	private static List<NameValuePair> getSendParams(String phone, String content, String UserName, String password) {
		EncryptTools encryptTools = new EncryptTools();
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("appId", SmsPlatformConstants.DL_ACCOUNT));
		nameValuePairs.add(new BasicNameValuePair("sign", SmsPlatformConstants.DL_SIGN));
		List<MtSmsReqInfoDetail> mtSmsReqInfoDetailList = new ArrayList<MtSmsReqInfoDetail>();

		for (int i = 0; i < 1; i++) {
			MtSmsReqInfoDetail mtSmsReqInfoDetail = new MtSmsReqInfoDetail();
			mtSmsReqInfoDetail.setMobile(phone);
			mtSmsReqInfoDetail.setSchtime("");
			mtSmsReqInfoDetail.setCustomSmsId(String.valueOf(System.currentTimeMillis()));
			mtSmsReqInfoDetail.setSmscontent(content);
			mtSmsReqInfoDetail.setExtendedCode("");
			mtSmsReqInfoDetail.setTempleteCode("");
			mtSmsReqInfoDetailList.add(mtSmsReqInfoDetail);

		}
		String s = JSON.toJSONString(mtSmsReqInfoDetailList);
		byte[] b = EncryptTools.encrypt(s, password);// AES鍔犲瘑
		String smsData = EncryptTools.parseByte2HexStr(b);
		nameValuePairs.add(new BasicNameValuePair("smsData", smsData));
		nameValuePairs.add(new BasicNameValuePair("mobile", phone));
		try {
			nameValuePairs.add(new BasicNameValuePair("account", UserName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nameValuePairs;
	}*/


    /**
     * @param phone    手机号
     * @param content  内容
     * @param userName 当啷用户名
     * @param password 当啷密码
     * @return
     * @Title: sendMessage
     * @Description: 发送短信
     * @date 2019年1月10日 下午3:44:14
     */
    public static Map<String, Object> sendMessage(String phone, String content, String userName,
                                                  String password, String account) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("appId", account);/*SmsPlatformConstants.DL_ACCOUNT*/
        byte[] encryptResult = EncryptTool.encrypt(System.currentTimeMillis() + "", password);
        String encryptResultStr = EncryptTool.parseByte2HexStr(encryptResult);
        params.put("sign", encryptResultStr);
        //params.put("account", userName);
        List<MtSmsReqInfoDetail> mtSmsReqInfoDetailList = new ArrayList<MtSmsReqInfoDetail>();
        MtSmsReqInfoDetail mtSmsReqInfoDetail = new MtSmsReqInfoDetail();
        mtSmsReqInfoDetail.setMobile(phone);
        mtSmsReqInfoDetail.setSchtime("");
        mtSmsReqInfoDetail.setCustomSmsId(String.valueOf(System.currentTimeMillis()));
        mtSmsReqInfoDetail.setSmscontent(content);
        mtSmsReqInfoDetail.setExtendedCode("");
        mtSmsReqInfoDetail.setTempleteCode("");
        mtSmsReqInfoDetailList.add(mtSmsReqInfoDetail);
        String s = JSON.toJSONString(mtSmsReqInfoDetailList);
        byte[] b = EncryptTool.encrypt(s, password);// AES鍔犲瘑
        String smsData = EncryptTool.parseByte2HexStr(b);
        params.put("smsData", smsData);
        String smsId = "";
        int state = 2;
        Map<String, Object> map = new HashMap<>();
        try {
            String json = HttpClientUtil.postRequest(SmsPlatformConstants.DL_SMS_URL_HTTPS, params);
            logger.info(json);
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject.getString("code").equals("SUCCESS")) {
                JSONArray jsonArray = (JSONArray) jsonObject.get("smses");
                smsId = jsonArray.getJSONObject(0).get("smsId").toString();
                state = 1;
            }
        } catch (Exception e) {
        }
        map.put(SmsPlatformConstants.SMS_SMSID, smsId);
        map.put(SmsPlatformConstants.SMS_STATE, state);
        return map;
    }
}
